import { defineStore } from 'pinia'
import { useContextMenuStore } from '@/stores/desk/contextMenu'
import { getImageUrl } from '@/utils/utils'

export type DestAppIconRange = 'win' | 'ios'
export type DeskAppIconSize = 'min' | 'mid' | 'big'
export type DeskAppIconSort = 'name' | 'size' | 'class' | 'date'
export type DeskBackgroundType = 'color' | 'image' | 'play'
export type DeskBackgroundImageType = 'fill' | 'contain' | 'cover'
export type DeskTheme = 'dark' | 'light'

export interface ISystemSetting {
  currentDate: Date
  systemVolume: number // 系统音量
  systemMuted: boolean // 系统禁音
  mainDesk: [number, number, 0 | 1] // 窗口id [outWidth, outHeight, isMainWin: 0是 1不是]
  deskAppRangeType: DestAppIconRange // 桌面app图标显示方式
  deskAppIconSize: DeskAppIconSize // 桌面app图标大小
  deskAppIconSort: DeskAppIconSort // 桌面app图标排序方式
  deskAppIconHidden: boolean // 桌面app图标隐藏
  deskLock: boolean // 是否锁屏
  deskProjection: 'copy' | 'expand' | 'first' | 'second' // 桌面投影方式
  deskWallpaper: string // 桌面壁纸
  deskWallpaperColor: string // 桌面壁纸
  deskBackgroundType: DeskBackgroundType // 桌面背景类型
  deskBackgroundImageType: DeskBackgroundImageType // 桌面背景类型
  deskTheme: DeskTheme // 桌面主题
}

let dateTimer: ReturnType<typeof setTimeout> | null = null
export const useSystemSettingStore = defineStore('SystemSetting', {
  state: (): ISystemSetting => ({
    currentDate: new Date(),
    systemVolume: 100,
    systemMuted: false,
    mainDesk: [0, 0, 0],
    deskAppRangeType: 'win',
    deskAppIconSize: 'min',
    deskAppIconSort: 'name',
    deskAppIconHidden: false,
    deskLock: true,
    deskProjection: 'expand',
    deskWallpaper: getImageUrl('bg2.jpg'),
    deskWallpaperColor: 'rgb(255,140,0)',
    deskBackgroundType: 'image',
    deskBackgroundImageType: 'cover',
    deskTheme: 'dark'
  }),
  actions: {
    refreshDate() {
      dateTimer && clearTimeout(dateTimer)
      dateTimer = setTimeout(() => {
        this.currentDate = new Date()
        this.refreshDate()
      }, 1000)
    },
    // 设置系统音量
    setSystemVolume(volume: number) {
      this.systemVolume = volume
    },
    // 设置系统禁音
    setVoiceMuted() {
      this.systemMuted = !this.systemMuted
    },
    // 设置窗口
    setMainDesk(width: number, height: number, isMain: 0 | 1 = 0) {
      this.mainDesk = [width, height, isMain]
    },
    // 修改图标大小
    setDeskAppSize(size: DeskAppIconSize) {
      this.deskAppIconSize = size
    },
    // 修改图标排序
    setDeskAppSort(sort: DeskAppIconSort) {
      this.deskAppIconSort = sort
    },
    // 隐藏图标
    setDeskAppHidden() {
      this.deskAppIconHidden = !this.deskAppIconHidden
      const menuStore = useContextMenuStore()
      menuStore.setDeskAppHidden(this.deskAppIconHidden)
    },
    // 切换图标排列类型
    setDestAppRangeType() {
      this.deskAppRangeType = this.deskAppRangeType === 'win' ? 'ios' : 'win'
      const menuStore = useContextMenuStore()
      menuStore.setDeskAppCheckRange(this.deskAppRangeType === 'win')
      location.reload()
    },
    // 锁屏窗口设置
    setDeskLock(isLock: boolean) {
      this.deskLock = isLock
    },
    // 设置背景图
    setDeskBackgroundWallpaper(Wallpaper: string) {
      this.deskWallpaper = Wallpaper
    },
    // 设置背景图
    setDeskBackgroundWallpaperColor(color: string) {
      this.deskWallpaperColor = color
    },
    // 设置背景类型
    setDeskBackgroundType(type: DeskBackgroundType) {
      this.deskBackgroundType = type
    },
    // 设置背景缩放类型
    setDeskBackgroundImageType(type: DeskBackgroundImageType) {
      this.deskBackgroundImageType = type
    },
    // 设置主题
    setDeskTheme(theme: DeskTheme) {
      this.deskTheme = theme
    }
  },
  persist: [
    {
      paths: [
        'systemVolume',
        'systemMuted',
        'mainDesk',
        'deskTheme',
        'deskAppRangeType',
        'deskAppIconSize',
        'deskAppIconSort',
        'deskAppIconHidden',
        'deskProjection',
        'deskWallpaper',
        'deskWallpaperColor',
        'deskBackgroundType',
        'deskBackgroundImageType'
      ],
      storage: localStorage
    },
    {
      paths: ['deskLock'],
      storage: sessionStorage
    }
  ]
})
